/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OrderList.java
 *
 * Created on 2009-12-6, 17:55:58
 */

package com.ordering.menu;

import com.ordering.dao.TOrderHeadJpaController;
import com.ordering.pojo.TOrderHead;
import com.ordering.pojo.UserManage;
import com.ordering.utils.WindowLocationUtils;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author hua
 */
public class OrderList extends javax.swing.JDialog {

    /** Creates new form OrderList */
    public OrderList(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        WindowLocationUtils.hideColumn(orderTable, 4);
        WindowLocationUtils.hideColumn(orderTable, 5);
        WindowLocationUtils.setWindowCenter(this);
        initTable();
    }
    private UserManage user;

    public UserManage getUser() {
        return user;
    }

    public void setUser(UserManage user) {
        this.user = user;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        orderTable = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        btnView = new javax.swing.JMenu();
        btnFinish = new javax.swing.JMenu();
        btnHasFinished = new javax.swing.JMenu();
        btnNoFinish = new javax.swing.JMenu();
        btnRefresh = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("订单管理");

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        orderTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "订单号", "订单类型", "会员卡号", "实收", "订单状态", "序号"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Float.class, java.lang.String.class, java.lang.Long.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        orderTable.setName("orderTable"); // NOI18N
        jScrollPane1.setViewportView(orderTable);

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        btnView.setText("查看");
        btnView.setName("btnView"); // NOI18N
        btnView.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnViewMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnView);

        btnFinish.setText("结算");
        btnFinish.setName("btnFinish"); // NOI18N
        btnFinish.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnFinishMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnFinish);

        btnHasFinished.setText("已结算");
        btnHasFinished.setName("btnHasFinished"); // NOI18N
        btnHasFinished.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnHasFinishedMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnHasFinished);

        btnNoFinish.setText("未结算");
        btnNoFinish.setName("btnNoFinish"); // NOI18N
        btnNoFinish.setVisible(false);
        btnNoFinish.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnNoFinishMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnNoFinish);

        btnRefresh.setText("刷新");
        btnRefresh.setName("btnRefresh"); // NOI18N
        btnRefresh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnRefreshMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnRefresh);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnViewMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnViewMouseClicked
        int rowIndex = orderTable.getSelectedRow();
        if(rowIndex==-1)
           JOptionPane.showMessageDialog(this, "请选择行");
        else{
           Object vipId = ((DefaultTableModel)orderTable.getModel()).getValueAt(rowIndex,5);
           if(vipId!=null){
                TOrderHeadJpaController control = new TOrderHeadJpaController();
                TOrderHead order = control.findTOrderHead((Long)vipId);
                OrderEdit editWin = new OrderEdit(order,"VIEW");
                editWin.setVisible(true);
           }
        }
    }//GEN-LAST:event_btnViewMouseClicked

    private void btnFinishMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnFinishMouseClicked
         int rowIndex = orderTable.getSelectedRow();
        if(rowIndex==-1)
           JOptionPane.showMessageDialog(this, "请选择行");
        else{
           Object vipId = ((DefaultTableModel)orderTable.getModel()).getValueAt(rowIndex,5);
           if(vipId!=null){
                TOrderHeadJpaController control = new TOrderHeadJpaController();
                TOrderHead order = control.findTOrderHead((Long)vipId);
                OrderEdit editWin = new OrderEdit(order,"EDIT");
                 editWin.addWindowListener(new java.awt.event.WindowAdapter() {
                            public void windowClosed(java.awt.event.WindowEvent e) {
                                initTable();
                            }
                        });
                editWin.setUser(user);
                editWin.setVisible(true);
           }
        }
    }//GEN-LAST:event_btnFinishMouseClicked

    private void btnHasFinishedMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnHasFinishedMouseClicked
        btnNoFinish.setVisible(true);
        btnHasFinished.setVisible(false);
        btnFinish.setVisible(false);
        initTable();
    }//GEN-LAST:event_btnHasFinishedMouseClicked

    private void btnNoFinishMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnNoFinishMouseClicked
        btnNoFinish.setVisible(false);
        btnHasFinished.setVisible(true);
        btnFinish.setVisible(true);
        initTable();
    }//GEN-LAST:event_btnNoFinishMouseClicked

    private void btnRefreshMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnRefreshMouseClicked
        initTable();
    }//GEN-LAST:event_btnRefreshMouseClicked
    public void initTable(){
        for(int rowIndex=orderTable.getRowCount()-1;rowIndex>-1;rowIndex--){
            ((DefaultTableModel)orderTable.getModel()).removeRow(rowIndex);
        }
        TOrderHeadJpaController control = new TOrderHeadJpaController();
        String sql = "select object(o) from TOrderHead as o ";
        if(!btnNoFinish.isVisible())
            sql+="where o.orderStatus='100'";//100默认值 已下单
        else
            sql+="where o.orderStatus='200'";//200 已结算
        List<TOrderHead> orders = control.findTOrderHeadEntitiesBySQL(sql);
        Object[] rowData = new Object[orderTable.getColumnCount()] ;
        for(TOrderHead order:orders){
            rowData[0] = order.getOrderNo();
            rowData[1] = order.getOrderType();
            rowData[2] = order.getCardNum();
            rowData[3] = order.getActualPrice();
            rowData[4] = order.getOrderStatus();
            rowData[5] = order.getId();
            ((DefaultTableModel)orderTable.getModel()).addRow(rowData);
        }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                OrderList dialog = new OrderList(new javax.swing.JFrame(), false);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosed(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu btnFinish;
    private javax.swing.JMenu btnHasFinished;
    private javax.swing.JMenu btnNoFinish;
    private javax.swing.JMenu btnRefresh;
    private javax.swing.JMenu btnView;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable orderTable;
    // End of variables declaration//GEN-END:variables

}
